package com.maidou.qiantai.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.maidou.qiantai.vo.spu.SpuInfoVo;
import com.maidou.qiantai.vo.spu.SpuRecommendVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.maidou.qiantai.domain.Spu;
import com.maidou.qiantai.service.ISpuService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品Controller
 * 
 * @author maidou
 * @date 2024-03-13
 */
@RestController
@RequestMapping("/qiantai/spu")
public class SpuController extends BaseController
{
    @Autowired
    private ISpuService spuService;

    /**
     * 商品推荐，查询商品列表
     */
    @GetMapping("/recommend-list")
    public TableDataInfo list(@RequestParam(name = "keyword",defaultValue="") String keyword)
    {
        startPage(); //为此方法添加返回值
        List<SpuRecommendVo> list = spuService.selectRecommendSpuList(keyword);
        TableDataInfo dataTable = getDataTable(list);
        SpuRecommendVo spuRecommendVo = list.get(0);
        Long total = spuRecommendVo.getNum();
        dataTable.setTotal(total);
        list.remove(0);
        return dataTable;
    }
    /**
     * 查询商品列表 根据分类id
     */
    @GetMapping("/catalog-list")
    public TableDataInfo listCatalogType()
    {
        startPage();
        List<SpuRecommendVo> list = spuService.selectCatalogSpuList();
        return getDataTable(list);
    }

    /**
     * 查询商品详情信息
     */
    @GetMapping("/spuInfo/{spuId}")
    public AjaxResult spuInfo(
            @PathVariable("spuId") Long spuId)
    {
        SpuInfoVo spuInfoVo = spuService.selectSpuInfo(spuId);
        return AjaxResult.success(spuInfoVo);
    }

    /**
     * 导出商品列表
     */
    @PreAuthorize("@ss.hasPermi('qiantai:spu:export')")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Spu spu)
    {
        List<Spu> list = spuService.selectSpuList(spu);
        ExcelUtil<Spu> util = new ExcelUtil<Spu>(Spu.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('qiantai:spu:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(spuService.selectSpuById(id));
    }

    /**
     * 新增商品
     */
    @PreAuthorize("@ss.hasPermi('qiantai:spu:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Spu spu)
    {
        return toAjax(spuService.insertSpu(spu));
    }

    /**
     * 修改商品
     */
    @PreAuthorize("@ss.hasPermi('qiantai:spu:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Spu spu)
    {
        return toAjax(spuService.updateSpu(spu));
    }

    /**
     * 删除商品
     */
    @PreAuthorize("@ss.hasPermi('qiantai:spu:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(spuService.deleteSpuByIds(ids));
    }
}
